package org.itboys.ace.web.lasted;

import org.itboys.admin.entity.lasted.SystemMenu;
import org.itboys.admin.service.lasted.SystemMenuService;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *作者：江雄
 * 描述：系统-菜单
 * 时间：2017年2月24日.重新构建
 */

@RestController
@RequestMapping("/ace/system/menu")
public class AceSystemMenuController extends BaseController {

	@Autowired
	private SystemMenuService menuService;


	/**
	 * 跳转菜单列表页面
	 * 
	 * @return
	 */
	@RequestMapping("/menu-list")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response, Model model) {
		return new ModelAndView("/ace/sys/menu/menu-list");
	}

	/**
	 * 获取菜单列表
	 * 
	 * @param request
	 * @param response
	 */
	@ResponseBody
	@RequestMapping("/getTree")
	public void getTree(HttpServletRequest request, HttpServletResponse response) {
		//Map<String, Object> param = QueryParamUtils.builderQueryMap(request);
		List<SystemMenu> page = menuService.getTree(null);
		AjaxUtils.renderJson(response, page);
	}






	/***
	 * 根据id查询菜单信息
	 * 
	 * @param id
	 * @param response
	 */
	@RequestMapping("/findOne")
	public void getOrg(@RequestParam(value="id",required = true)Long id, HttpServletResponse response) {
		AjaxUtils.renderJson(response, menuService.getById(id));
	}

	/**
	 * 新增/修改菜单
	 * 
	// * @param am
	 */
	@ResponseBody
	@RequestMapping(value = "/save")
	public void add(@ModelAttribute SystemMenu memnu, HttpServletResponse response) {
			try{
				menuService.saveMemnu(memnu);
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("SystemMenu save error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}

	/**
	 * 根据菜单ID，删除一条数据
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public void delete(@RequestParam(value="id",required = true)Long id, HttpServletResponse response) {
		try {
			menuService.deleteMenu(id);
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("SystemMenu delete error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}

}
